package com.gopay.scheduler.job;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.gopay.common.core.dic.dao.DicCurrencyTypeDAO;
import com.gopay.common.domain.cbrmb.exchange.ResponseT;
import com.gopay.common.domain.dic.DicCurrencyType;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.eis.exchange.AirWallexExchangeRemoteService;
import com.gopay.scheduler.GopayBaseJob;

/**
 * @ClassName: CbRmbExRateHolidayUpdateJob
 * @Description: 更新AIRWALLEX提供的今日可换汇币种
 * @author huangcheng
 * @date 2017年7月20日 下午3:34:40
 */
public class CbRmbQueryExRateHolidayUpdateJob extends GopayBaseJob {
    // 日志
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    DicCurrencyTypeDAO currencyTypeDAO;

    @Resource(name = "exchangeRemoteServiceClient")
    private AirWallexExchangeRemoteService exchangeRemoteServiceClient;

    @Override
    protected void doJobInternal() {
        try {
            logger.info("更新AIRWALLEX提供的今日可换汇币种任务开始！");
            List<DicCurrencyType> allCurrencyList = currencyTypeDAO.getAllCurrencyList();
            for (Iterator iterator = allCurrencyList.iterator(); iterator.hasNext();) {
                DicCurrencyType dicCurrencyType = (DicCurrencyType) iterator.next();
                ResponseT<String> responseT = exchangeRemoteServiceClient.invalidConversionDates("CNY" + dicCurrencyType.getCurCode());
                if (responseT.getCode() == 200) {
                    String content = responseT.getContent();
                    String date = DateUtils.format(new Date(), DateUtils.yyyy_MM_dd);
                    if (content.contains(date)) {
                        dicCurrencyType.setEffective("0");
                    } else {
                        dicCurrencyType.setEffective("1");
                    }
                    dicCurrencyType.setLastUpdTs(new Date());
                    currencyTypeDAO.update(dicCurrencyType);
                }
            }
            logger.info("更新AIRWALLEX提供的今日可换汇币种任务结束！");
        } catch (Exception e) {
            logger.error("更新AIRWALLEX提供的今日可换汇币种任务出错！", e);
        }
    }
}
